/**
 * @file hooks useTime
 */

import { ref, computed, onBeforeUnmount } from 'vue';
/**
 *
 * @param {*} maxSeconds 毫秒数
 * @param {*} onCallbackTimeout 超时时回调函数
 * @param {*} sessionStorageKey 时间缓存key值
 */
const useTime = (maxSeconds, onCallbackTimeout, sessionKey = '') => {
  // 缓存是否有值
  const newSeconds = sessionStorage.getItem(sessionKey) || maxSeconds;
  // 计时器
  const time = ref(+newSeconds);

  // 定时器
  let timer = null;

  const timeResult = computed(() => {
    let timeValue = time.value;
    let m, s;
    timeValue = timeValue > 0 ? timeValue : 0;
    m = parseInt(timeValue / 60);
    s = timeValue % 60;
    m = m > 9 ? m : `0${m}`;
    s = s > 9 ? s : `0${s}`;
    return `${m}:${s}`;
  });

  // 方法-取消
  const clear = (isRemove = false) => {
    clearInterval(timer);
    // 是否移除缓存
    isRemove && sessionStorage.removeItem(sessionKey);
  };
  // 方法-开始计时
  const start = (isDown = true) => {
    timer = setInterval(() => {
      // 结束时
      if (isDown && time.value === 0) {
        clear(true);
        onCallbackTimeout && onCallbackTimeout();
        return;
      }
      // 是否自减
      if (isDown) {
        time.value--;
      } else {
        time.value++;
      }
      // 需要本地缓存
      if (sessionKey) {
        sessionStorage.setItem(sessionKey, time.value);
      }
    }, 1000);
  };
  // 方法-自增
  const startUp = () => {
    start(false);
  };
  // 方法-重置
  const reset = () => {
    time.value = maxSeconds;
  };

  onBeforeUnmount(() => {
    clear(true);
  });

  return {
    time: timeResult,
    startDown: start,
    startUp,
    reset,
    clear
  };
};
export default useTime;
